﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;

namespace QuanLyThuNgan
{
    public partial class frm_DM_NhomVSGPB : DevExpress.XtraEditors.XtraForm
    {
        public frm_DM_NhomVSGPB()
        {
            InitializeComponent();
        }

        string sTenSearch = "";
        string sColSearch;
        string sColRow;

        DMNhomVSGPBCTL nhomVSCtl = new DMNhomVSGPBCTL();
        DMNhomVSGPBDTO nhomVSDto = new DMNhomVSGPBDTO();

        private void frm_DM_NhomVSGPB_Load(object sender, EventArgs e)
        {
            DataTable dtloadNhomVS = nhomVSCtl.m_SelectAllDMNhomVSGPB();
            gridControlNhomVSGPB.DataSource = dtloadNhomVS;

            gvNhomVSGPB.OptionsBehavior.ReadOnly = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = false;
            gvNhomVSGPB.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)gridControlNhomVSGPB.DataSource;
            if (dt != null)
            {
                for (int i = 0; i < gvNhomVSGPB.RowCount; i++)
                {
                    DataRow dr = gvNhomVSGPB.GetDataRow(i);
                    gvNhomVSGPB.MoveNext();
                    gvNhomVSGPB.MovePrev();
                    if (dr != null)
                    {
                        nhomVSDto.STenNhomVSGPB = dr["TenNhomVSGPB"].ToString().Trim();
                        nhomVSDto.STenVni = Uit.it_Font.UnicodeToVNI(dr["TenNhomVSGPB"].ToString().Trim());
                        
                        nhomVSDto.BbNgungSuDung = Uit.it_Parse.ToBoolean(dr["bNgungSuDung"].ToString());
                        if (dr["IDNhomVSGPB"].ToString() == "")
                        {
                            nhomVSCtl.m_InsertDMNhomVSGPB(nhomVSDto);
                        }
                        else
                        {
                            nhomVSDto.IIDNhomVSGPB = Uit.it_Parse.ToInteger(dr["IDNhomVSGPB"].ToString());
                            nhomVSCtl.m_UpdateDMNhomVSGPB(nhomVSDto);
                        }
                    }
                }
                
            }

            XtraMessageBox.Show("Lưu thành công");
            DataTable dtloadVS = nhomVSCtl.m_SelectAllDMNhomVSGPB();
            gridControlNhomVSGPB.DataSource = dtloadVS;

            btnSave.Enabled = false;
            btnUpdate.Enabled = true;
            btnInsert.Enabled = true;
            gvNhomVSGPB.OptionsBehavior.ReadOnly = true;
            gvNhomVSGPB.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            gvNhomVSGPB.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = false;
            btnInsert.Enabled = false;
            gvNhomVSGPB.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            //reset load du lieu----------------------
            gvNhomVSGPB.ActiveFilterEnabled = false;
            gvNhomVSGPB.ClearColumnsFilter();
            gvNhomVSGPB.OptionsFilter.Reset();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            gvNhomVSGPB.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnInsert.Enabled = true;
            btnSave.Enabled = false;
            gvNhomVSGPB.OptionsBehavior.ReadOnly = true;
            DataTable dtloadVS = nhomVSCtl.m_SelectAllDMNhomVSGPB();
            gridControlNhomVSGPB.DataSource = dtloadVS;
            gvNhomVSGPB.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            //reset load du lieu----------------------
            gvNhomVSGPB.ActiveFilterEnabled = false;
            gvNhomVSGPB.ClearColumnsFilter();
            gvNhomVSGPB.OptionsFilter.Reset();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataRow dr = gvNhomVSGPB.GetDataRow(gvNhomVSGPB.FocusedRowHandle);
            if (dr != null)
            {
                nhomVSDto.IIDNhomVSGPB = Uit.it_Parse.ToInteger(dr["IDNhomVSGPB"].ToString());
                nhomVSCtl.m_DeleteDMNhomVSGPB(nhomVSDto);

            }
            XtraMessageBox.Show("Xóa thành công");
            DataTable dtloadVS = nhomVSCtl.m_SelectAllDMNhomVSGPB();
            gridControlNhomVSGPB.DataSource = dtloadVS;
            //reset load du lieu----------------------
            gvNhomVSGPB.ActiveFilterEnabled = false;
            gvNhomVSGPB.ClearColumnsFilter();
            gvNhomVSGPB.OptionsFilter.Reset();
        }

        private void gvNhomVSGPB_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = gvNhomVSGPB;
            GridColumn colNhomVS = view.Columns["TenNhomVSGPB"];
            int iRowIndex = e.RowHandle;
            //if (sColRow != "bNgungSuDung")
            //{

            string sTenNhomVS = view.GetRowCellValue(e.RowHandle, colNhomVS).ToString().ToUpper().Trim();
            if (sTenNhomVS == "")
            {
                e.Valid = false;
                view.SetColumnError(colNhomVS, "Tên nhóm VSGPB không được trống");
            }

            //set vong lap kiem tra gia tri thay doi-------------------------
            for (int i = 0; i < gvNhomVSGPB.RowCount; i++)
            {
                if (i != iRowIndex)
                {
                    DataRow drVS = gvNhomVSGPB.GetDataRow(i);
                    string sTestNhomVS = drVS["TenNhomVSGPB"].ToString().ToUpper().Trim();
                    if (sTenNhomVS == sTestNhomVS)
                    {
                        e.Valid = false;
                        XtraMessageBox.Show("Tên nhóm VSPGPB không được trùng","Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        view.SetColumnError(colNhomVS, "Tên nhóm VSPGPB không được trùng");
                        return;
                    }
                }
            }
            //}
            btnSave.Enabled = true;
        }


        private void gvNhomVSGPB_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e)
        {
            sColRow = e.FocusedColumn.FieldName;
        }
        //tim ten can search---------------------------------------------
        private void gvNhomVSGPB_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            sTenSearch = e.Value.ToString();
            sColSearch = e.Column.FieldName;
        }
        //set ten can tim----------------------------------------------------------------
        private void gvNhomVSGPB_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gvNhomVSGPB;
            view.ActiveFilter.Add(view.Columns[sColSearch], new ColumnFilterInfo("[" + sColSearch + "] Like '%" + sTenSearch + "%'", ""));
        
        }


    }
}